package com.example.myokhttp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    List<Joke> jokeLists;
    JokesAdapter jokesAdapter; // 要是全局的

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initData();

    }

    private void initView(){

        RecyclerView jokeList = findViewById(R.id.rv_main);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        jokeList.setLayoutManager(linearLayoutManager);
        jokesAdapter = new JokesAdapter(MainActivity.this,jokeLists);
        jokeList.setAdapter(jokesAdapter);

    }


    private void initData(){

        OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .callTimeout(100,TimeUnit.SECONDS)
                .readTimeout(100,TimeUnit.SECONDS)
                .build();
        Request request = new Request.Builder()
                .get()
                .url("http://api.apishop.net/common/joke/getJokesByRandom?apiKey=dQEjXma3f768313cca3c971ae5d89d745a2ecb366d15287&pageSize=20")
                .build();

        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                String error = e.getMessage();

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {

                String data = response.body().string();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                         //使用json 解析
                        JSONObject jsonObject = JSON.parseObject(data);
                        JSONObject jsonObject2 = JSON.parseObject(jsonObject.getString("result"));
                        // 这里使用json工具类返回一个list
                        jokeLists= JsonTool.jsonToList(jsonObject2.getString("Object"),Joke.class);

//                        JSONObject jsonObject = JSON.parseObject(data);
//                        JSONArray jsonArray = jsonObject.getJSONArray("result");
//                        for(int i=0;i<jsonArray.size();i++){
//                            jsonArray.get(i);
//                            jokeLists= JsonTool.jsonToList((String) jsonArray.get(i),Joke.class);
//                        }

                        jokesAdapter.notifyDataSetChanged(); // 数据有变化，一定要更新适配器

                    }

                });


            }
        });

    }




}